﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using RawLevelFormat;
using theguineasyndrome.editor;
using Microsoft.Xna.Framework.Graphics;
using System.IO;

namespace theguineasyndrome.levels
{
    abstract public class LevelLoader
    {
        protected GameMode gameMode;
        protected ContentManager content;

        protected WaterWeapon waterWeapon;
        protected FireWeapon fireWeapon;
        protected PortalWeapon portalWeapon;
        protected PaintWeapon paintWeapon;
        protected Level level;

        abstract public Level load();

        public LevelLoader(GameMode gameMode, ContentManager content, WaterWeapon waterWeapon, FireWeapon fireWeapon, PortalWeapon portalWeapon, PaintWeapon paintWeapon)
        {
            this.gameMode = gameMode;
            this.content = content;

            this.waterWeapon = waterWeapon;
            this.fireWeapon = fireWeapon;
            this.portalWeapon = portalWeapon;
            this.paintWeapon = paintWeapon;
        }

        protected Level getLevel(string name)
        {
            RawLevel rl = content.Load<RawLevel>(name);
            return LevelConverter.toLevel(rl, content);
        }

        protected Level getCustomLevel(string name)
        {
            string home = Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%");
            string tgsDir = home + "\\the_guinea_syndrome";
            string fullName = tgsDir + "\\" + name + ".lvl";
            RawLevel rl = RawLevel.fromBytes(File.ReadAllBytes(fullName));
            return LevelConverter.toLevel(rl, content);
        }

        protected void addCarots(Level level)
        {
            int i = 1;
            Texture2D carotTexture = content.Load<Texture2D>("images/carots/carot");
            while (level.pointMarkExists("carot " + i))
            {
                level.addElement(new Carot(level, carotTexture, null, level.getPointMarkPosition("carot " + i)));
                i++;
            }

            i = 1;
            Texture2D marioCarotTexture = content.Load<Texture2D>("images/carots/mario");
            while (level.pointMarkExists("mario carot " + i))
            {
                level.addElement(new Carot(level, marioCarotTexture, new MarioModel(content, waterWeapon), level.getPointMarkPosition("mario carot " + i)));
                i++;
            }

            i = 1;
            Texture2D portalCarotTexture = content.Load<Texture2D>("images/carots/portal");
            while (level.pointMarkExists("portal carot " + i))
            {
                level.addElement(new Carot(level, portalCarotTexture, new PortalModel(content, portalWeapon), level.getPointMarkPosition("portal carot " + i)));
                i++;
            }

            i = 1;
            Texture2D pyroCarotTexture = content.Load<Texture2D>("images/carots/pyro");
            while (level.pointMarkExists("pyro carot " + i))
            {
                level.addElement(new Carot(level, pyroCarotTexture, new PyroModel(content, fireWeapon), level.getPointMarkPosition("pyro carot " + i)));
                i++;
            }

            Texture2D endCarotTexture = content.Load<Texture2D>("images/carots/endcarot");
            level.addElement(new EndCarot(level, endCarotTexture, level.getPointMarkPosition("end")));
        }
    }
}
